package top.yinjinbiao.arcface.common.rpc;

import lombok.Data;
import top.yinjinbiao.arcface.common.exception.ErrorCodeEnum;

/**
 * @Description 通用的返回对象
 * @Author yin.jinbiao
 * @Date 2021/9/26 15:52
 * @Version 1.0
 */
@Data
public class ResponseResult<T> {

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 返回对象
     */
    private T data;

    public static <T> ResponseResult<T> ok(){
        return newResponse(ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg(), null);
    }

    public static <T> ResponseResult<T> ok(T data){
        return newResponse(ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg(), data);
    }

    public static <T> ResponseResult<T> newResponse(int code, String msg,T data) {
        ResponseResult<T> result = new ResponseResult<T>();
        result.setCode(code);
        result.setMsg(msg);
        if (data != null && data instanceof String && "".equals(data)) {
            result.setData(null);
        } else {
            result.setData(data);
        }
        return result;
    }
}
